//
//  PlayView.m
//  27-Block练习播放按钮
//
//  Created by 王道林 on 16/8/19.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"

@interface PlayView ()

@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *lastButton;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);

@end


@implementation PlayView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self =[super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}


- (void)setUp
{
    _playButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [_playButton setImage:[UIImage imageNamed:@"hp_player_btn_play_normal"] forState:UIControlStateNormal];
    
    [_playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextButton setImage:[UIImage imageNamed:@"hp_player_btn_next_highlight"] forState:UIControlStateNormal];
    _lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_lastButton setImage:[UIImage imageNamed:@"hp_player_btn_pre_highlight"] forState:UIControlStateNormal];
    
    [self addSubview:_playButton];
    [self addSubview:_nextButton];
    [self addSubview:_lastButton];
    
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    _lastButton.wdl_width = 60;
    _lastButton.wdl_height = 60;
    _lastButton.center_y = _playButton.center_y;
    _lastButton.center_x = _playButton.center_x - (_lastButton.wdl_width / 2 + _playButton.wdl_width / 2 + 30);
    
    _nextButton.wdl_width = 60;
    _nextButton.wdl_height = 60;
    _nextButton.center_y = _playButton.center_y;
    _nextButton.center_x = _playButton.center_x + (_nextButton.wdl_width / 2 + _playButton.wdl_width / 2);
    
}



- (void)play:(UIButton*)playButton
{
    [playButton setImage:[UIImage imageNamed:@"hp_player_btn_pause_normal"] forState:UIControlStateNormal];
    [playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [playButton addTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
    
    self.play(playButton);
}

- (void)pasue:(UIButton *)playButton
{
    [playButton setImage:[UIImage imageNamed:@"hp_player_btn_play_normal"] forState:UIControlStateNormal];
    [playButton removeTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
    [playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(playButton);
}


- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause
{
    self.play = play;
    self.pause = pause;
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
